package conson.tech.manage.app.controller.mobile;


import conson.tech.manage.api.app.constant.AppCommon;

import javax.servlet.http.HttpServletRequest;


public class BaseController {


    /**
     * 获取header中存放的userId
     *
     * @param request
     * @return
     */
    protected Long getCurrentUserId(HttpServletRequest request) {

        String uid = request.getHeader(AppCommon.USER_ID);

        return uid == null ? null : Long.valueOf(uid);
    }


    /**
     * 获取header中存放的username
     *
     * @param request
     * @return
     */
    protected String getCurrentUsername(HttpServletRequest request) {

        return request.getHeader(AppCommon.USER_NAME);

    }

    /**
     * 获取header中存放的UserMobile
     *
     * @param request
     * @return
     */
    protected String getCurrentUserMobile(HttpServletRequest request) {

        return request.getHeader(AppCommon.USER_MOBILE);

    }


    /**
     * 参数校验
     *
     * @param params
     * @return
     */
    protected boolean paramCheck(Object... params) {
        if (params == null || params.length == 0) {
            return true;
        }

        for (Object o : params) {
            if (o == null || o.toString().equals("")) {
                return false;
            }
        }

        return true;
    }

    /**
     * 解析accessToken
     *
     * @param request
     * @return
     */
    protected String getAccessToken(HttpServletRequest request) {
        final String TOKEN_PREFIX = "Bearer ";
        final String TOKEN_HEADER = "authorization";

        String accessToken = request.getHeader(TOKEN_HEADER);

        return accessToken == null ? null : accessToken.substring(TOKEN_PREFIX.length() - 1).trim();

    }


}
